import load from '../../templates/loader/loader.js'
import api from '../../../api/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrayData: [],
    drift: false,
    toppos: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //初始化载入等待动画
    that.loader = load.loader(that, {
      data: "loader",
      borderWidth: 1
    });
    var data = {};
    var array = decodeURI(decodeURI(options.keyWord))
    var arrayObj = JSON.parse(array)
    // for (var i in jsonObj) {
    //   var array = [];
    //   array.push(jsonObj[i].id)
    //   data[i] = array
    // }
    var arrayStr = arrayObj.join(",")
    console.log(arrayStr)
    var json = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"spIds":"' + arrayStr + '","id":"' + options.species_id+'"}}'
    that.loader.open()
    api.getGoodsList({
      method: 'POST',
      data: {
        REQ_MESSAGE: json
      },
      success: (res) => {
        that.loader.close(); //关闭载入动画
        if (res.rows) {
          if (res.rows.length <= 0) {
            that.loader.open("没有更多了");
          } else {
            that.setData({
              arrayData: res.rows
            })
          }
        } else {
          that.loader.open("没有更多了");
        }

      },
      fail: (res) => {
        that.loader.open(api.statusFail.title);
      }
    });
  },
  onPageScroll: function (e) {
    if (e.scrollTop < this.data.toppos) {
      if (this.data.drift) {
        this.setData({
          drift: false
        })
      }
    }
    this.data.toppos = e.scrollTop;
  },
  onReachBottom: function () {
    this.setData({
      drift: true
    })
  }
})